package com.pkk.components.rpc.response.util;

import com.pkk.components.rpc.common.code.CommonCode;
import com.pkk.components.rpc.enums.CodeEnum;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.ResponseHeader;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @description: 返回响应工具类
 * @author: peikunkun
 * @create: 2019-07-02 17:02
 **/
@Data
@NoArgsConstructor
public class ResponseUtil {


  /**
   * @Description: 成功
   * @Param: []
   * @return: com.pkk.components.rpc.response.CommonResponse<T>
   * @Author: peikunkun
   * @Date: 2019/7/3 0003 上午 11:04
   */
  public static <T> CommonResponse<T> success() {
    return new CommonResponse(create(CommonCode.SUCCESS), (Object) null);
  }

  /**
   * @Description: 成功返回
   * @Param: [t]
   * @return: com.pkk.components.rpc.response.CommonResponse<T>
   * @Author: peikunkun
   * @Date: 2019/7/3 0003 上午 11:05
   */
  public static <T> CommonResponse<T> success(T t) {
    return new CommonResponse(create(CommonCode.SUCCESS), t);
  }

  public static <T> CommonResponse<T> error() {
    return new CommonResponse(create(CommonCode.FAIL), (Object) null);
  }

  public static <T> CommonResponse<T> error(CodeEnum codedEnum) {
    return new CommonResponse(create(codedEnum), (Object) null);
  }

  public static <T> CommonResponse<T> error(CodeEnum codedEnum, String message) {
    return new CommonResponse(createError(codedEnum, message), (Object) null);
  }

  public static <T> CommonResponse<T> error(String message) {
    return new CommonResponse(createErrorMessage(message), (Object) null);
  }

  public static <T> CommonResponse<T> error(String code, String message) {
    return new CommonResponse(createError(code, message), (Object) null);
  }

  private static ResponseHeader create(CodeEnum errorCode) {
    return new ResponseHeader(errorCode.getCode(), errorCode.getMsg());
  }

  private static ResponseHeader createError(CodeEnum code, String message) {
    return new ResponseHeader(code.getCode(), message);
  }

  private static ResponseHeader createError(String code, String message) {
    return new ResponseHeader(code, message);
  }

  private static ResponseHeader createErrorMessage(String message) {
    return new ResponseHeader(CommonCode.FAIL.getCode(), message);
  }

}
